from django.db.models import Max

import requests

from django.forms import forms

from django.shortcuts import render, redirect

from django.contrib import messages

from django.views.decorators.csrf import csrf_exempt

from django.http import HttpResponse, HttpResponseRedirect

from User.models import Userinfo


# Create your views here.

def hello(request):
    return HttpResponse("Hello world ! ")


def index(request):
    return render(request, 'index.html')  # 首页


def gameSingle(request):
    return render(request, 'game-single.html')  # 游戏详细页


def discover(request):
    return render(request, 'discover.html')  # 发现


def ranklist(request):
    return render(request, 'ranklist.html')  # 排行榜


def about(request):
    return render(request, 'about.html')  # 关于


# 登录处理
@csrf_exempt  # 关闭csrf验证
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    if request.method == 'POST':
        phone = request.POST.get("phone")
        password = request.POST.get("password")
        # 把表单中取到的值和数据库里做对比
        user1 = Userinfo.objects.filter(phone=phone)
        if user1.count() == 0:
            messages.error(request, '用户不存在！')
            return render(request, 'login.html')
        if password == user1.values_list('password', flat=True)[0]:
            return HttpResponse("<script>alert('登录成功！');location.href='/index'</script>")
            # return render(request, 'index.html', {'message': '登录成功！！'})
        else:
            messages.error(request, '密码错误，请重新登录！')
            return render(request, 'login.html')


# 注册
def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    if request.method == 'POST':
        # userid= Userinfo.objects.all().aggregate(Max('userid')).values_list("userid_max","flat=True")
        # 前端数据
        userid = Userinfo.objects.latest("userid").userid + 1
        data = request.POST
        name = data.get('name')
        phone = data.get('phone')
        age = data.get('age')
        gender = data.get('gender')
        profession = data.get('profession')
        password = data.get('password1')
        # 数据库数据
        user = Userinfo.objects.filter(phone=phone)
        if user.exists():
            messages.error(request, '手机号已存在！')
            return render(request, 'register.html')
        test1 = Userinfo(userid=userid, name=name, password=password, gender=gender,
                         age=age, profession=profession, phone=phone)
        test1.save()
        return HttpResponse("<script>alert('注册成功，请登录！');location.href='/login'</script>")
        # messages.success(request, '注册成功，请登录！')
        # return render(request, 'login.html')


# 忘记密码处理
def forget(request):
    if request.method == 'GET':
        return render(request, 'forget.html')
    if request.method == 'POST':
        # 前端数据
        phone = request.POST.get("phone")
        password = request.POST.get("password")
        # 数据库数据
        user = Userinfo.objects.filter(phone=phone)
        if user.exists():
            user.update(password=password)
            return HttpResponse("<script>alert('密码修改成功，请登录！');location.href='/register'</script>")
            # return render(request, 'register.html', {'message': '密码修改成功，请登录！'})
        else:
            messages.error(request, '手机号不存在！')
            return redirect('/forget')
            # return render(request, 'forget.html')


def logout(request):
    pass


# 数据库测试
def testdb(request):
    # 字段名需要同数据库中一致
    test1 = Userinfo(UserID="2", name="li", password="123", phone="123123")
    test1.save()
    return HttpResponse('<p>数据添加成功</p>')
